package xyz.wpxy.iotlink;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import xyz.wpxy.iotlink.network.mqtt.config.MqttBrokerProvider;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
@SpringBootApplication
public class IotLinkSystemApplication implements CommandLineRunner {

    @Autowired(required = false)
    private MqttBrokerProvider mqttBrokerProvider;


    public static void main(String[] args) {
        SpringApplication.run(IotLinkSystemApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {

        ExecutorService executorService = Executors.newCachedThreadPool();

        executorService.execute(() -> {
            if (mqttBrokerProvider == null) {
                log.info("mqttbroker未配置启动");
                return;
            }
            try {
                mqttBrokerProvider.create();
                log.info("mqttbroker启动成功");
            } catch (Exception e) {
                log.error("mqtt创建失败", e);
            }
        });

    }
}
